﻿using System;
using System.IO;
using NLog;

namespace Labthink.PCSoft.UniversalHost4.DataAccess
{
    public class DataManage
    {
        public static readonly string BasePath = AppDomain.CurrentDomain.BaseDirectory;
        private static readonly Logger _logger = LogManager.GetCurrentClassLogger();

        public static bool IsGmp { get; set; }

        /// <summary>
        /// 是否试验中
        /// </summary>
        public static bool IsTest { get; set; }

        public static void Init()
        {
            IsTest = false;

            //备份还原配置
            //BackupAndRestore.Init();

            //获取设备信息
            BaseRepository.GetDeviceId();

            //加载单位转换关系
            CurrentRepository.Load();

            //启动数据盾客户端
            DataShieldClient.Start();
        }

        /// <summary>
        /// 恢复出厂设置
        /// </summary>
        public static void Recover()
        {
            // 删除运行日志
            var dir = Path.Combine(BasePath, "logs");
            if (Directory.Exists(dir))
            {
                Directory.Delete(dir, true);
            }

            _logger.Info("删除运行日志完成");

            //删除试验记录
            dir = Path.Combine(BasePath, "Db");
            if (Directory.Exists(dir))
            {
                Directory.Delete(dir, true);
            }

            _logger.Info("删除试验记录完成");

            // 重建数据库
            dir = Path.Combine(BasePath, "Config");
            var bakDir = Path.Combine(BasePath, "Config_bak");
            if (Directory.Exists(bakDir))
            {
                var dbBak = Path.Combine(bakDir, "App.db");
                if (File.Exists(dbBak))
                {
                    File.Copy(dbBak, dir + "\\App.db", true);
                    _logger.Info("重建主数据库完成");
                }

                var dbOpLogBak = Path.Combine(bakDir, "OperationLogs.db");
                if (File.Exists(dbOpLogBak))
                {
                    File.Copy(dbOpLogBak, dir + "\\OperationLogs.db", true);
                    _logger.Info("重建操作日志数据库完成");
                }

                //恢复默认配置
                //var cfgBak = Path.Combine(bakDir, "AppConfig.ini");
                //if (File.Exists(cfgBak))
                //{
                //    File.Copy(cfgBak, dir + "\\AppConfig.ini", true);
                //    Log.Debug().Message("恢复默认配置完成").Write();
                //}

                var idBak = Path.Combine(bakDir, "id.ini");
                if (File.Exists(idBak))
                {
                    File.Copy(idBak, dir + "\\id.ini", true);
                    _logger.Info("重置试验Id完成");
                }
            }

            // 清除DataShield缓存文件
            dir = Path.Combine(BasePath, "DataShield");
            if (Directory.Exists(dir))
            {
                Directory.Delete(dir, true);
            }

            // 清除临时文件
            dir = "Temp";
            if (Directory.Exists(dir))
            {
                Directory.Delete(dir, true);
                _logger.Info("删除临时文件完成");
            }
        }
    }
}
